//
//  AppLogic.m
//  gamebox_qpp
//
//  Created by 赵隆杰 on 2021/12/10.
//

#import "AppLogic.h"
#import "MainViewController.h"
#import "LoginViewController.h"
#import "ZCBaseNavigationController.h"


@implementation AppLogic

+ (instancetype)sharedInstance {
    static AppLogic *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[AppLogic alloc] init];
    });
    return instance;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.isApple = YES;
    }
    
    return self;
}

- (void)rootControllerSwitchToMainVC {
    MainViewController *mainVC = [MainViewController new];
    [UIApplication sharedApplication].delegate.window.rootViewController = mainVC;
}

- (void)rootControllerSwitchToLoginVC {
    
    LoginViewController *login_rootVC = [LoginViewController new];
    ZCBaseNavigationController *login_nav = [[ZCBaseNavigationController alloc] initWithRootViewController:login_rootVC];
    [UIApplication sharedApplication].delegate.window.rootViewController = login_nav;
    
}

- (void)pushToWebViewVC:(NSString *)webTitle url:(NSString *)webUrl {
    [self pushToWebViewVC:webTitle url:webUrl isHiddenNavi:NO isClearNavi:NO];
}

- (void)pushToWebViewVC:(NSString *)webTitle url:(NSString *)webUrl isClearNavi:(BOOL)isClearNavi {
    [self pushToWebViewVC:webTitle url:webUrl isHiddenNavi:NO isClearNavi:YES];
}

- (void)pushToWebViewVC:(NSString *)webTitle url:(NSString *)webUrl isHiddenNavi:(BOOL)isHiddenNavi isClearNavi:(BOOL)isClearNavi {
    ZCWebViewController *vc = [[ZCWebViewController alloc] init];
    vc.url = webUrl;
    vc.titleName = webTitle;
    vc.isHiddenNavi = isHiddenNavi;
    vc.isClearNavi = isClearNavi;
    [[UIWindow currentViewController].navigationController pushViewController:vc animated:YES];
}

- (void)presentLoginVC {
    __block UIViewController *currentVC = [UIWindow currentViewController];
//    if (![currentVC isKindOfClass:[BaseViewController class]]) {
//        return;
//    }
//    if ([currentVC isKindOfClass:[LoginBaseViewController class]]) {
//        return;
//    }
    if ([LocalData fetchUserInfo]) {
        return;
    }
    LoginViewController *vc = [[LoginViewController alloc] init];
    vc.loginCompleteBlock = ^(BOOL result) {
        if (result) {//登录成功
            ZCBaseViewController *tempVC = (ZCBaseViewController *)currentVC;
            [tempVC refreshAfterLogin];
        } else {
            if (currentVC.navigationController.viewControllers.count > 1) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{//延迟pop，防止tabbar消失
                    [currentVC.navigationController popViewControllerAnimated:YES];
                });
            } else if (currentVC.presentingViewController) {
                [currentVC dismissViewControllerAnimated:YES completion:nil];
            }
        }
    };
    ZCBaseNavigationController *naviVC = [[ZCBaseNavigationController alloc] initWithRootViewController:vc];
    naviVC.modalPresentationStyle = UIModalPresentationOverFullScreen;

    [currentVC presentViewController:naviVC animated:YES completion:nil];
}

//- (BOOL)isApple
//{
//    return NO;
//}




@end
